package com.sandau.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sandau.backend.VO.PageResult;
import com.sandau.backend.entity.Student;
import com.sandau.backend.mapper.StudentMapper;
import com.sandau.backend.service.IStudentService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张青
 * @since 2025-06-10
 */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private IStudentService studentService;
    @Autowired
    private StudentMapper studentMapper;

    @RequestMapping("/list")
    public PageResult<Student> list(String page, String limit) {
        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setCode(0);
        pageResult.setMsg("查询成功");
        pageResult.setCount(1000);
        pageResult.setData(studentService.list());
        return pageResult;
    }

    @RequestMapping("/page_list")
    public PageResult<Student> page(Long page, Long limit) {
        Page<Student> pageStudent = new Page<>(page, limit);
        Page<Student> resultPage = studentMapper.selectPage(pageStudent, null);

        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setCode(0);
        pageResult.setMsg("查询成功");
        pageResult.setCount((int) resultPage.getTotal());
        pageResult.setData(resultPage.getRecords());
        return pageResult;
    }

    @RequestMapping("/add")
    public String add() {
        Student student = new Student();
        student.setName("张三");
        return studentService.save(student) ? "添加成功" : "添加失败";
    }

}
